package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ArticleCreateHtmlService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.constants.ArticleConstants;
import com.heima.model.search.pojos.SearchArticleVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class ArticleCreateHtmlServiceImpl implements ArticleCreateHtmlService {

    @Autowired
    private Configuration configuration;

    @Autowired
    private ApArticleContentMapper contentMapper;
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private ApArticleMapper apArticleMapper;


    @Async
    @Override
    public void ArticleTOMinIo(Long articleId) throws IOException, TemplateException {

        //根据文章的id查询文章的内容
        ApArticleContent articleContent = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery()
                .eq(ApArticleContent::getArticleId, articleId));

        //获取freemarker的模板对象并生成一个静态的html
        Template template = configuration.getTemplate("article.ftl");

        StringWriter writer = new StringWriter();
        //数据模型
        Map<String, Object> params = new HashMap<>();
        params.put("context", JSONArray.parseArray(articleContent.getContent()));
        //合成
        template.process(params,writer);

        //3.把html上传到minio中并返回访问的地址
        InputStream inputStream = new ByteArrayInputStream(writer.toString().getBytes());
        String path = fileStorageService.uploadHtmlFile("", articleContent.getArticleId() + ".html", inputStream);

        //把访问的地址更新到ap_article表中
        ApArticle entity = new ApArticle();
        entity.setId(articleContent.getArticleId());
        entity.setStaticUrl(path);
        apArticleMapper.updateById(entity);

        //发送消息到MQ
        createArticleData(entity,articleContent.getContent(),path);


    }
    @Autowired
    private KafkaTemplate<String ,String > kafkaTemplate;


    private void createArticleData(ApArticle entity, String content, String path) {
        SearchArticleVo articleVo = new SearchArticleVo();
        BeanUtils.copyProperties(entity,articleVo);
        articleVo.setContent(content);
        articleVo.setStaticUrl(path);

        //发送消息
        kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(articleVo));
    }
}
